/*
 * MultiUnpackerView.java
 */

package multiunpacker;

import java.awt.Dimension;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.Task;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

/**
 * The application's main frame.
 */
public class MultiUnpackerView extends FrameView {

    public MultiUnpackerView(SingleFrameApplication app) {
        super(app);
        initComponents();
        getFrame().setMinimumSize(new Dimension(468, 327));
        getFrame().setIconImage(new ImageIcon(getClass().getClassLoader().getResource("multiunpacker/resources/winrar.gif")).getImage());

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = MultiUnpackerApp.getApplication().getMainFrame();
            aboutBox = new MultiUnpackerAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        MultiUnpackerApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        rootFolder = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        chooseRootFolder = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        winRarExec = new javax.swing.JTextField();
        chooseWinrarExec = new javax.swing.JButton();
        extractAll = new javax.swing.JButton();
        overwrite = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        outputFolder = new javax.swing.JTextField();
        choseOutput = new javax.swing.JButton();
        outputSame = new javax.swing.JCheckBox();
        deleteAfter = new javax.swing.JCheckBox();
        extractNested = new javax.swing.JCheckBox();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setAutoscrolls(true);
        mainPanel.setMinimumSize(new java.awt.Dimension(468, 267));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(468, 267));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(multiunpacker.MultiUnpackerApp.class).getContext().getResourceMap(MultiUnpackerView.class);
        rootFolder.setText(resourceMap.getString("rootFolder.text")); // NOI18N
        rootFolder.setName("rootFolder"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(multiunpacker.MultiUnpackerApp.class).getContext().getActionMap(MultiUnpackerView.class, this);
        chooseRootFolder.setAction(actionMap.get("chooseRootFolder")); // NOI18N
        chooseRootFolder.setText(resourceMap.getString("chooseRootFolder.text")); // NOI18N
        chooseRootFolder.setName("chooseRootFolder"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        winRarExec.setText(resourceMap.getString("winRarExec.text")); // NOI18N
        winRarExec.setToolTipText(resourceMap.getString("winRarExec.toolTipText")); // NOI18N
        winRarExec.setName("winRarExec"); // NOI18N

        chooseWinrarExec.setAction(actionMap.get("chooserWinRarExec")); // NOI18N
        chooseWinrarExec.setText(resourceMap.getString("chooseWinrarExec.text")); // NOI18N
        chooseWinrarExec.setName("chooseWinrarExec"); // NOI18N

        extractAll.setAction(actionMap.get("extractAllPackages")); // NOI18N
        extractAll.setText(resourceMap.getString("extractAll.text")); // NOI18N
        extractAll.setToolTipText(resourceMap.getString("extractAll.toolTipText")); // NOI18N
        extractAll.setName("extractAll"); // NOI18N

        overwrite.setText(resourceMap.getString("overwrite.text")); // NOI18N
        overwrite.setToolTipText(resourceMap.getString("overwrite.toolTipText")); // NOI18N
        overwrite.setName("overwrite"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        outputFolder.setText(resourceMap.getString("outputFolder.text")); // NOI18N
        outputFolder.setEnabled(false);
        outputFolder.setName("outputFolder"); // NOI18N

        choseOutput.setAction(actionMap.get("chooseOutputFolder")); // NOI18N
        choseOutput.setText(resourceMap.getString("choseOutput.text")); // NOI18N
        choseOutput.setEnabled(false);
        choseOutput.setName("choseOutput"); // NOI18N

        outputSame.setAction(actionMap.get("changeSameAsArchive")); // NOI18N
        outputSame.setSelected(true);
        outputSame.setText(resourceMap.getString("outputSame.text")); // NOI18N
        outputSame.setToolTipText(resourceMap.getString("outputSame.toolTipText")); // NOI18N
        outputSame.setName("outputSame"); // NOI18N

        deleteAfter.setText(resourceMap.getString("deleteAfter.text")); // NOI18N
        deleteAfter.setToolTipText(resourceMap.getString("deleteAfter.toolTipText")); // NOI18N
        deleteAfter.setName("deleteAfter"); // NOI18N

        extractNested.setText(resourceMap.getString("extractNested.text")); // NOI18N
        extractNested.setToolTipText(resourceMap.getString("extractNested.toolTipText")); // NOI18N
        extractNested.setName("extractNested"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(extractNested)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(overwrite, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(outputSame, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(outputFolder, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                                    .addComponent(winRarExec, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                                    .addComponent(rootFolder, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGap(10, 10, 10)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(chooseRootFolder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(choseOutput, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(chooseWinrarExec, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(deleteAfter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 198, Short.MAX_VALUE)
                        .addComponent(extractAll)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rootFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chooseRootFolder))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(choseOutput))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(outputSame)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(winRarExec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chooseWinrarExec))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(overwrite)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(extractNested)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 4, Short.MAX_VALUE)
                        .addComponent(extractAll)
                        .addContainerGap())
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteAfter)
                        .addContainerGap())))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setMinimumSize(new java.awt.Dimension(468, 27));
        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 448, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(44, Short.MAX_VALUE))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void chooseRootFolder() {
        try {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(File.listRoots()[0]);
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileHidingEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            int res = chooser.showOpenDialog(rootFolder);
            if (res == JFileChooser.APPROVE_OPTION) {
                File chosen = chooser.getSelectedFile();
                if (!chosen.isDirectory()) {
                    chosen = chosen.getParentFile();
                }
                rootFolder.setText(chosen.getPath());
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
     }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @Action(block = Task.BlockingScope.WINDOW)
    public Task extractAllPackages() {
        return new ExtractAllPackagesTask(getApplication());
    }

    private class ExtractAllPackagesTask extends org.jdesktop.application.Task<Object, Void> {

        private int complete = 1;

        ExtractAllPackagesTask(org.jdesktop.application.Application app) {
            // Runs on the EDT.  Copy GUI state that
            // doInBackground() depends on from parameters
            // to ExtractAllPackagesTask fields, here.
            super(app);
        }

        @Override protected Object doInBackground() {
            if (rootFolder.getText().trim().equals("")) {
                return null;
            }
            setProgress(complete);
            File root = new File(rootFolder.getText());
            final List<File> files = new ArrayList<File>();
            findRarFiles(root, files);
            for (File rar : files) {
                unrar(rar);
                complete += 1;
                setProgress(complete, 1, files.size()+1);
            }
            if (extractNested.isSelected()) {
                final List<File> nestedFiles = new ArrayList<File>();
                final List<File> toExtract = new ArrayList<File>();
                do {
                    findRarFiles(root, nestedFiles);
                    toExtract.clear();
                    for (File nested : nestedFiles) {
                        if (!files.contains(nested)) {
                            toExtract.add(nested);
                            files.add(nested);
                        }
                    }
                    for (File toEx : toExtract) {
                        unrar(toEx);
                        complete += 1;
                        setProgress(complete, 1, files.size()+1);
                    }
                } while (toExtract.size() > 0);

            }
            // Your Task's code here.  This method runs
            // on a background thread, so don't reference
            // the Swing GUI from here.
            return null;  // return your result
        }

        private void deleteArchive(File child) {
            if (child.getName().toLowerCase().endsWith(".rar")) {
                for (File suspect : child.getParentFile().listFiles()) {
                    if (suspect.getName().toLowerCase().matches(".*\\.r[0-9]{2}")) {
                        suspect.delete();
                    }
                }
            }
            child.delete();
        }

        private void findRarFiles(File root, List<File> rars) {
            for (File rar : root.listFiles()) {
                if (rar.isDirectory()) {
                    findRarFiles(rar, rars);
                } else {
                    if (rar.getName().toLowerCase().endsWith(".rar")
                            || rar.getName().toLowerCase().endsWith(".zip")) {
                        rars.add(rar);
                    }
                }
            }
        }

        @Override protected void succeeded(Object result) {
            // Runs on the EDT.  Update the GUI based on
            // the result computed by doInBackground().
            JOptionPane.showMessageDialog(mainPanel, "Extracted " + complete
                    + " archives!", "Done!", JOptionPane.INFORMATION_MESSAGE);
        }

        private void unrar(File child) {
            Process x = null;
            try {
                List<String> commands = new ArrayList<String>();
                commands.add(winRarExec.getText());
                String opts = "x";
                commands.add(opts);
                commands.add("-y");
                if (overwrite.isSelected()) {
                    commands.add("-o+");
                } else {
                    commands.add("-o-");
                }
                commands.add(child.getName());
                if (!outputSame.isSelected() && (outputFolder.getText().trim().length() > 0)) {
                    commands.add(outputFolder.getText());
                }
                String[] cmd = new String[commands.size()];
                commands.toArray(cmd);
                x = Runtime.getRuntime().exec(cmd, null, child.getParentFile());
                int exitCode = x.waitFor();
                if (exitCode == 0 && deleteAfter.isSelected()) {
                    deleteArchive(child);
                }
            } catch (InterruptedException ex) {
                if (x != null) {
                    x.destroy();
                }
                Logger.getLogger(MultiUnpackerView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MultiUnpackerView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Action
    public void chooserWinRarExec() {
        try {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(File.listRoots()[0]);
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileHidingEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setSelectedFile(new File(winRarExec.getText()));
            chooser.setFileFilter(new FileFilter() {
                @Override
                public boolean accept(File f) {
                    return f.isDirectory() || f.getName().toLowerCase().equals("winrar.exe");
                }
                @Override
                public String getDescription() {
                    return "WinRAR Executable";
                }
            });
            int res = chooser.showOpenDialog(rootFolder);
            if (res == JFileChooser.APPROVE_OPTION) {
                File chosen = chooser.getSelectedFile();
                winRarExec.setText(chosen.getPath());
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }

    @Action
    public void changeSameAsArchive() {
        choseOutput.setEnabled(!outputSame.isSelected());
        outputFolder.setEnabled(!outputSame.isSelected());
    }

    @Action
    public void chooseOutputFolder() {
        try {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(File.listRoots()[0]);
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileHidingEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            int res = chooser.showOpenDialog(rootFolder);
            if (res == JFileChooser.APPROVE_OPTION) {
                File chosen = chooser.getSelectedFile();
                if (!chosen.isDirectory()) {
                    chosen = chosen.getParentFile();
                }
                outputFolder.setText(chosen.getPath());
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseRootFolder;
    private javax.swing.JButton chooseWinrarExec;
    private javax.swing.JButton choseOutput;
    private javax.swing.JCheckBox deleteAfter;
    private javax.swing.JButton extractAll;
    private javax.swing.JCheckBox extractNested;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTextField outputFolder;
    private javax.swing.JCheckBox outputSame;
    private javax.swing.JCheckBox overwrite;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JTextField rootFolder;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JTextField winRarExec;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
